<?php
 
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
 
class SimpleImage {
 
   var $image;
   var $image_type;
 
   function load($filename) {
 
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
 
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
 
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
 
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
 
         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image,$filename);
      }
      if( $permissions != null) {
 
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
 
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {
 
         imagepng($this->image);
      }
   }
   function getWidth() {
 
      return imagesx($this->image);
   }
   function getHeight() {
 
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
 
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
 
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
 
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }
 
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   } 
    function grayscale($imgw,$imgh){
		$new_image = $this -> image;
		for ($i=0; $i<$imgw; $i++)
		{		
			for ($j=0; $j<$imgh; $j++)
			{
					// get the rgb value for current pixel
					$rgb = ImageColorAt($new_image, $i, $j);
					// extract each value for r, g, b
					$rr = ($rgb >> 16) & 0xFF;
					$gg = ($rgb >> 8) & 0xFF;
					$bb = $rgb & 0xFF;
					// get the Value from the RGB value
					$g = round(($rr + $gg + $bb) / 3);
					// grayscale values have r=g=b=g
					$val = imagecolorallocate($new_image, $g, $g, $g);
					// set the gray value
					imagesetpixel ($new_image, $i, $j, $val);
			}
		}
		$this->image = $new_image;
   }
    function resize_ratio($width,$height) {
	
	$ratio1=($this->getWidth())/$width;
    $ratio2=($this->getHeight())/$height;
     if($ratio1>$ratio2)    {
         $thumb_w=$width;
         $thumb_h=($this->getHeight())/$ratio1;
     }
     else    {
         $thumb_h=$height;
         $thumb_w=($this->getWidth())/$ratio2;
     }	
      $new_image = imagecreatetruecolor($thumb_w, $thumb_h);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $thumb_w, $thumb_h, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   } 
	function crop_image($width,$height){
		$this -> resize_ratio(300,300);
		$new_image =  $this->image;
		$cropped_image_gd = imagecreatetruecolor($width, $height);
		$original_width = $this -> getWidth();
		$original_height = $this -> getHeight();
		$wm = $original_width /$width;
		$hm = $original_height /$height;
		$h_height = $height/2;
		$w_height = $width/2;

		if($original_width > $original_height )
		{
		$adjusted_width =$original_width / $hm;
		$half_width = $adjusted_width / 2;
		$int_width = $half_width - $w_height;

		imagecopyresampled($cropped_image_gd ,$new_image ,-$int_width,0,0,0, $adjusted_width, $height, $original_width , $original_height );
		}
		elseif(($original_width < $original_height ) || ($original_width == $original_height ))
		{
		$adjusted_height = $original_height / $wm;
		$half_height = $adjusted_height / 2;
		$int_height = $half_height - $h_height;

		imagecopyresampled($cropped_image_gd , $new_image ,0,-$int_height,0,0, $width, $adjusted_height, $original_width , $original_height );
		}
		else {
		imagecopyresampled($cropped_image_gd ,$new_image ,0,0,0,0, $width, $height, $original_width , $original_height );
		}
		$this -> image = $cropped_image_gd;
	}
}
?>